package com.sunreal.paygateway.common;


import java.util.concurrent.atomic.AtomicLong;

import com.sunreal.paygateway.sys.util.DateUtil;

/**
 * @author dingzhiwei jmdhappy@126.com
 * @version V1.0
 * @Description: 生成全局唯一序列号工具类
 * @date 2017-07-05
 * @Copyright: www.xxpay.org
 */
public class MySeq {

    private static AtomicLong pay_seq = new AtomicLong(0L);
    private static AtomicLong trans_seq = new AtomicLong(0L);
    private static AtomicLong refund_seq = new AtomicLong(0L);

    public static String getPay(String mchid) {
        String pay_seq_prefix = "1";
        return getSeq(pay_seq_prefix, pay_seq, mchid);
    }

    public static String getTrans(String mchid) {
        String trans_seq_prefix = "2";
        return getSeq(trans_seq_prefix, trans_seq, mchid);
    }

    public static String getRefund(String mchid) {
        String refund_seq_prefix = "9";
        return getSeq(refund_seq_prefix, refund_seq, mchid);
    }

    private static String getSeq(String prefix, AtomicLong seq, String mchid) {
        return String.format("%s%s%s%05d", prefix, mchid, DateUtil.getSeqString(), (int) seq.getAndIncrement() % 1000);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            System.out.println("pay=" + getPay("101"));
            //            System.out.println("trans=" + getTrans());
            //            System.out.println("refund=" + getRefund());
        }

    }

}
